package com.boot.controller;

import com.boot.entity.User;
import com.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RegisterController {

    @Autowired
    UserService userService;

    @GetMapping("/register")
    public String register(){
        return "manager/register";
    }

    @PostMapping("/doRegister")
    @ResponseBody
    public String doRegister(@RequestBody User user){

        List<User> users = userService.getByUserName(user.getUserName());

        if(users!=null && users.size()>0){
            return "已存在用户";
        }else{
            userService.saveOrUpdate(user);
        }

        return "success";
    }

}
